All files / remote persistent_stream.ts

96.59% Statements 198/205
88.24% Branches 45/51
95.83% Functions 46/48
96.41% Lines 188/195
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765                                            2x 2x 2x 2x   2x       2x     2x                       2x           2x             2x           2x             2x               2x         2x                                               2x     2x   2x     2x                                                                 2x           1237x 1237x       1237x     1237x   1237x 1237x 1237x   1237x             1237x                   2x 3989x                     2x 2978x                   2x 793x 60x 60x     733x 733x 733x                 2x 1231x 611x                       2x 56x   56x 56x                         1168x     1168x 548x     6x     548x         542x                     2x 2208x 2208x       2x 6x     6x           2x 2941x 548x 548x                                   2x       733x         733x   733x   617x 116x   2x 2x     2x       733x 615x 615x 615x         733x 733x     733x       733x 122x   611x               2x                                 733x 733x         733x   733x         733x                                     733x 733x         733x             733x       4563x   4563x 3933x   630x           733x 733x 733x 733x 733x 727x       727x 727x     733x 725x 116x     733x 3105x 3090x           60x 60x       60x   60x 60x   2x     58x 58x 58x 58x       2x 116x 116x   116x           116x   2x                                         2x                 617x   617x                 2x     392x           2x   2261x   2261x 2261x     2261x                 2x 511x 511x 511x   511x 511x 6x     511x             2x 300x 300x 300x 300x   2x                                                                         2x         620x           620x   620x                                             2x 380x       2x 355x 355x     2x 270x 261x       2x     341x           2x   829x       829x   829x   332x       332x 332x         497x   497x     497x     497x                 2x 336x 336x     336x 336x 336x       1062x 1062x 1062x       1061x         1061x       844x     1061x   2x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import * as api from '../protos/firestore_proto_api';
import { CredentialsProvider, Token } from '../api/credentials';
import { SnapshotVersion } from '../core/snapshot_version';
import { ProtoByteString, TargetId } from '../core/types';
import { QueryData } from '../local/query_data';
import { Mutation, MutationResult } from '../model/mutation';
import { assert } from '../util/assert';
import { AsyncQueue, TimerId } from '../util/async_queue';
import { Code, FirestoreError } from '../util/error';
import * as log from '../util/log';
 
import { ExponentialBackoff } from './backoff';
import { Connection, Stream } from './connection';
import { JsonProtoSerializer } from './serializer';
import { WatchChange } from './watch_change';
import { isNullOrUndefined } from '../util/types';
import { CancelablePromise } from '../util/promise';
 
const LOG_TAG = 'PersistentStream';
 
// The generated proto interfaces for these class are missing the database
// field. So we add it here.
// TODO(b/36015800): Remove this once the api generator is fixed.
interface ListenRequest extends api.ListenRequest {
  database?: string;
}
export interface WriteRequest extends api.WriteRequest {
  database?: string;
}
 
enum PersistentStreamState {
  /**
   * The streaming RPC is not running and there's no error condition.
   * Calling `start` will start the stream immediately without backoff.
   * While in this state isStarted will return false.
   */
  Initial,
 
  /**
   * The stream is starting, and is waiting for an auth token to attach to
   * the initial request. While in this state, isStarted will return
   * true but isOpen will return false.
   */
  Auth,
 
  /**
   * The streaming RPC is up and running. Requests and responses can flow
   * freely. Both isStarted and isOpen will return true.
   */
  Open,
 
  /**
   * The stream encountered an error. The next start attempt will back off.
   * While in this state isStarted() will return false.
   *
   */
  Error,
 
  /**
   * An in-between state after an error where the stream is waiting before
   * re-starting. After
   * waiting is complete, the stream will try to open. While in this
   * state isStarted() will return YES but isOpen will return false.
   */
  Backoff,
 
  /**
   * The stream has been explicitly stopped; no further events will be emitted.
   */
  Stopped
}
 
/**
 * Provides a common interface that is shared by the listeners for stream
 * events by the concrete implementation classes.
 */
export interface PersistentStreamListener {
  /**
   * Called after the stream was established and can accept outgoing
   * messages
   */
  onOpen: () => Promise<void>;
  /**
   * Called after the stream has closed. If there was an error, the
   * FirestoreError will be set.
   */
  onClose: (err?: FirestoreError) => Promise<void>;
}
 
/**
 * Initial backoff time in milliseconds after an error.
 * Set to 1s according to https://cloud.google.com/apis/design/errors.
 */
const BACKOFF_INITIAL_DELAY_MS = 1000;
 
/** Maximum backoff time in milliseconds */
const BACKOFF_MAX_DELAY_MS = 60 * 1000;
 
const BACKOFF_FACTOR = 1.5;
 
/** The time a stream stays open after it is marked idle. */
const IDLE_TIMEOUT_MS = 60 * 1000;
 
/**
 * A PersistentStream is an abstract base class that represents a streaming RPC
 * to the Firestore backend. It's built on top of the connections own support
 * for streaming RPCs, and adds several critical features for our clients:
 *
 *   - Exponential backoff on failure
 *   - Authentication via CredentialsProvider
 *   - Dispatching all callbacks into the shared worker queue
 *
 * Subclasses of PersistentStream implement serialization of models to and
 * from the JSON representation of the protocol buffers for a specific
 * streaming RPC.
 *
 * ## Starting and Stopping
 *
 * Streaming RPCs are stateful and need to be `start`ed before messages can
 * be sent and received. The PersistentStream will call the onOpen function
 * of the listener once the stream is ready to accept requests.
 *
 * Should a `start` fail, PersistentStream will call the registered
 * onClose with a FirestoreError indicating what went wrong.
 *
 * A PersistentStream can be started and stopped repeatedly.
 *
 * Generic types:
 *  SendType: The type of the outgoing message of the underlying
 *    connection stream
 *  ReceiveType: The type of the incoming message of the underlying
 *    connection stream
 *  ListenerType: The type of the listener that will be used for callbacks
 */
export abstract class PersistentStream<
  SendType,
  ReceiveType,
  ListenerType extends PersistentStreamListener
> {
  private state: PersistentStreamState;
  private inactivityTimerPromise: CancelablePromise<void> | null = null;
  private stream: Stream<SendType, ReceiveType> | null = null;
 
  protected backoff: ExponentialBackoff;
 
  protected listener: ListenerType | null = null;
 
  constructor(
    private queue: AsyncQueue,
    connectionTimerId: TimerId,
    private idleTimerId: TimerId,
    protected connection: Connection,
    private credentialsProvider: CredentialsProvider
  ) {
    this.backoff = new ExponentialBackoff(
      queue,
      connectionTimerId,
      BACKOFF_INITIAL_DELAY_MS,
      BACKOFF_FACTOR,
      BACKOFF_MAX_DELAY_MS
    );
    this.state = PersistentStreamState.Initial;
  }
 
  /**
   * Returns true if `start` has been called and no error has occurred. True
   * indicates the stream is open or in the process of opening (which
   * encompasses respecting backoff, getting auth tokens, and starting the
   * actual RPC). Use `isOpen` to determine if the stream is open and ready for
   * outbound requests.
   */
  isStarted(): boolean {
    return (
      this.state === PersistentStreamState.Backoff ||
      this.state === PersistentStreamState.Auth ||
      this.state === PersistentStreamState.Open
    );
  }
 
  /**
   * Returns true if the underlying RPC is open (the openHandler has been
   * called) and the stream is ready for outbound requests.
   */
  isOpen(): boolean {
    return this.state === PersistentStreamState.Open;
  }
 
  /**
   * Starts the RPC. Only allowed if isStarted returns false. The stream is
   * not immediately ready for use: onOpen will be invoked when the RPC is ready
   * for outbound requests, at which point isOpen will return true.
   *
   *  When start returns, isStarted will return true.
   */
  start(listener: ListenerType): void {
    if (this.state === PersistentStreamState.Error) {
      this.performBackoff(listener);
      return;
    }
 
    assert(this.state === PersistentStreamState.Initial, 'Already started');
    this.listener = listener;
    this.auth();
  }
 
  /**
   * Stops the RPC. This call is idempotent and allowed regardless of the
   * current isStarted state.
   *
   * When stop returns, isStarted and isOpen will both return false.
   */
  stop(): void {
    if (this.isStarted()) {
      this.close(PersistentStreamState.Stopped);
    }
  }
 
  /**
   * After an error the stream will usually back off on the next attempt to
   * start it. If the error warrants an immediate restart of the stream, the
   * sender can use this to indicate that the receiver should not back off.
   *
   * Each error will call the onClose function. That function can decide to
   * inhibit backoff if required.
   */
  inhibitBackoff(): void {
    assert(!this.isStarted(), 'Can only inhibit backoff in a stopped state');
 
    this.state = PersistentStreamState.Initial;
    this.backoff.reset();
  }
 
  /**
   * Marks this stream as idle. If no further actions are performed on the
   * stream for one minute, the stream will automatically close itself and
   * notify the stream's onClose() handler with Status.OK. The stream will then
   * be in a !isStarted() state, requiring the caller to start the stream again
   * before further use.
   *
   * Only streams that are in state 'Open' can be marked idle, as all other
   * states imply pending network operations.
   */
  markIdle(): void {
    // Starts the idle time if we are in state 'Open' and are not yet already
    // running a timer (in which case the previous idle timeout still applies).
    if (this.isOpen() && this.inactivityTimerPromise === null) {
      this.inactivityTimerPromise = this.queue.enqueueAfterDelay(
        this.idleTimerId,
        IDLE_TIMEOUT_MS,
        () => this.handleIdleCloseTimer()
      );
 
      this.inactivityTimerPromise.catch((err: FirestoreError) => {
        // When the AsyncQueue gets drained during testing, pending Promises
        // (including these idle checks) will get rejected. We special-case
        // these cancelled idle checks to make sure that these specific Promise
        // rejections are not considered unhandled.
        assert(
          err.code === Code.CANCELLED,
          `Received unexpected error in idle timeout closure. Expected CANCELLED, but was: ${
            err
          }`
        );
      });
    }
  }
 
  /** Sends a message to the underlying stream. */
  protected sendRequest(msg: SendType): void {
    this.cancelIdleCheck();
    this.stream!.send(msg);
  }
 
  /** Called by the idle timer when the stream should close due to inactivity. */
  private handleIdleCloseTimer(): Promise<void> {
    Eif (this.isOpen()) {
      // When timing out an idle stream there's no reason to force the stream into backoff when
      // it restarts so set the stream state to Initial instead of Error.
      return this.close(PersistentStreamState.Initial);
    }
    return Promise.resolve();
  }
 
  /** Marks the stream as active again. */
  private cancelIdleCheck() {
    if (this.inactivityTimerPromise) {
      this.inactivityTimerPromise.cancel();
      this.inactivityTimerPromise = null;
    }
  }
 
  /**
   * Closes the stream and cleans up as necessary:
   *
   * * closes the underlying GRPC stream;
   * * calls the onClose handler with the given 'error';
   * * sets internal stream state to 'finalState';
   * * adjusts the backoff timer based on the error
   *
   * A new stream can be opened by calling `start` unless `finalState` is set to
   * `PersistentStreamState.Stopped`.
   *
   * @param finalState the intended state of the stream after closing.
   * @param error the error the connection was closed with.
   */
  private close(
    finalState: PersistentStreamState,
    error?: FirestoreError
  ): Promise<void> {
    assert(
      finalState === PersistentStreamState.Error || isNullOrUndefined(error),
      "Can't provide an error when not in an error state."
    );
 
    this.cancelIdleCheck();
 
    if (finalState !== PersistentStreamState.Error) {
      // If this is an intentional close ensure we don't delay our next connection attempt.
      this.backoff.reset();
    } else if (error && error.code === Code.RESOURCE_EXHAUSTED) {
      // Log the error. (Probably either 'quota exceeded' or 'max queue length reached'.)
      log.error(error.toString());
      log.error(
        'Using maximum backoff delay to prevent overloading the backend.'
      );
      this.backoff.resetToMax();
    }
 
    // Clean up the underlying stream because we are no longer interested in events.
    if (this.stream !== null) {
      this.tearDown();
      this.stream.close();
      this.stream = null;
    }
 
    // This state must be assigned before calling onClose() to allow the callback to
    // inhibit backoff or otherwise manipulate the state in its non-started state.
    this.state = finalState;
    const listener = this.listener!;
 
    // Clear the listener to avoid bleeding of events from the underlying streams.
    this.listener = null;
 
    // If the caller explicitly requested a stream stop, don't notify them of a closing stream (it
    // could trigger undesirable recovery logic, etc.).
    if (finalState !== PersistentStreamState.Stopped) {
      return listener.onClose(error);
    } else {
      return Promise.resolve();
    }
  }
 
  /**
   * Can be overridden to perform additional cleanup before the stream is closed.
   * Calling super.tearDown() is not required.
   */
  protected tearDown(): void {}
 
  /**
   * Used by subclasses to start the concrete RPC and return the underlying
   * connection stream.
   */
  protected abstract startRpc(
    token: Token | null
  ): Stream<SendType, ReceiveType>;
 
  /**
   * Called after the stream has received a message. The function will be
   * called on the right queue and must return a Promise.
   * @param message The message received from the stream.
   */
  protected abstract onMessage(message: ReceiveType): Promise<void>;
 
  private auth(): void {
    assert(
      this.state === PersistentStreamState.Initial,
      'Must be in initial state to auth'
    );
 
    this.state = PersistentStreamState.Auth;
 
    this.credentialsProvider.getToken(/*forceRefresh=*/ false).then(
      token => {
        // Normally we'd have to schedule the callback on the AsyncQueue.
        // However, the following calls are safe to be called outside the
        // AsyncQueue since they don't chain asynchronous calls
        this.startStream(token);
      },
      (error: Error) => {
        this.queue.enqueue(() => {
          if (this.state !== PersistentStreamState.Stopped) {
            // Stream can be stopped while waiting for authorization.
            const rpcError = new FirestoreError(
              Code.UNKNOWN,
              'Fetching auth token failed: ' + error.message
            );
            return this.handleStreamClose(rpcError);
          } else {
            return Promise.resolve();
          }
        });
      }
    );
  }
 
  private startStream(token: Token | null): void {
    Iif (this.state === PersistentStreamState.Stopped) {
      // Stream can be stopped while waiting for authorization.
      return;
    }
 
    assert(
      this.state === PersistentStreamState.Auth,
      'Trying to start stream in a non-auth state'
    );
    // Helper function to dispatch to AsyncQueue and make sure that any
    // close will seem instantaneous and events are prevented from being
    // raised after the close call
    const dispatchIfStillActive = (
      stream: Stream<SendType, ReceiveType>,
      fn: () => Promise<void>
    ) => {
      this.queue.enqueue(() => {
        // Only raise events if the stream instance has not changed
        if (this.stream === stream) {
          return fn();
        } else {
          return Promise.resolve();
        }
      });
    };
 
    // Only start stream if listener has not changed
    Eif (this.listener !== null) {
      const currentStream = this.startRpc(token);
      this.stream = currentStream;
      this.stream.onOpen(() => {
        dispatchIfStillActive(currentStream, () => {
          assert(
            this.state === PersistentStreamState.Auth,
            'Expected stream to be in state auth, but was ' + this.state
          );
          this.state = PersistentStreamState.Open;
          return this.listener!.onOpen();
        });
      });
      this.stream.onClose((error: FirestoreError) => {
        dispatchIfStillActive(currentStream, () => {
          return this.handleStreamClose(error);
        });
      });
      this.stream.onMessage((msg: ReceiveType) => {
        dispatchIfStillActive(currentStream, () => {
          return this.onMessage(msg);
        });
      });
    }
  }
 
  private performBackoff(listener: ListenerType): void {
    assert(
      this.state === PersistentStreamState.Error,
      'Should only perform backoff in an error case'
    );
    this.state = PersistentStreamState.Backoff;
 
    this.backoff.backoffAndRun(() => {
      if (this.state === PersistentStreamState.Stopped) {
        // Stream can be stopped while waiting for backoff to complete.
        return Promise.resolve();
      }
 
      this.state = PersistentStreamState.Initial;
      this.start(listener);
      assert(this.isStarted(), 'PersistentStream should have started');
      return Promise.resolve();
    });
  }
 
  private handleStreamClose(error?: FirestoreError): Promise<void> {
    assert(this.isStarted(), "Can't handle server close on non-started stream");
    log.debug(LOG_TAG, `close with error: ${error}`);
 
    this.stream = null;
 
    // In theory the stream could close cleanly, however, in our current model
    // we never expect this to happen because if we stop a stream ourselves,
    // this callback will never be called. To prevent cases where we retry
    // without a backoff accidentally, we set the stream to error in all cases.
    return this.close(PersistentStreamState.Error, error);
  }
}
 
/** Listener for the PersistentWatchStream */
export interface WatchStreamListener extends PersistentStreamListener {
  /**
   * Called on a watchChange. The snapshot parameter will be MIN if the watch
   * change did not have a snapshot associated with it.
   */
  onWatchChange: (
    watchChange: WatchChange,
    snapshot: SnapshotVersion
  ) => Promise<void>;
}
 
/**
 * A PersistentStream that implements the Listen RPC.
 *
 * Once the Listen stream has called the openHandler, any number of listen and
 * unlisten calls calls can be sent to control what changes will be sent from
 * the server for ListenResponses.
 */
export class PersistentListenStream extends PersistentStream<
  api.ListenRequest,
  api.ListenResponse,
  WatchStreamListener
> {
  constructor(
    queue: AsyncQueue,
    connection: Connection,
    credentials: CredentialsProvider,
    private serializer: JsonProtoSerializer
  ) {
    super(
      queue,
      TimerId.ListenStreamConnection,
      TimerId.ListenStreamIdle,
      connection,
      credentials
    );
  }
 
  protected startRpc(
    token: Token | null
  ): Stream<api.ListenRequest, api.ListenResponse> {
    return this.connection.openStream<api.ListenRequest, api.ListenResponse>(
      'Listen',
      token
    );
  }
 
  protected onMessage(watchChangeProto: api.ListenResponse): Promise<void> {
    // A successful response means the stream is healthy
    this.backoff.reset();
 
    const watchChange = this.serializer.fromWatchChange(watchChangeProto);
    const snapshot = this.serializer.versionFromListenResponse(
      watchChangeProto
    );
    return this.listener!.onWatchChange(watchChange, snapshot);
  }
 
  /**
   * Registers interest in the results of the given query. If the query
   * includes a resumeToken it will be included in the request. Results that
   * affect the query will be streamed back as WatchChange messages that
   * reference the targetId.
   */
  watch(queryData: QueryData): void {
    const request: ListenRequest = {};
    request.database = this.serializer.encodedDatabaseId;
    request.addTarget = this.serializer.toTarget(queryData);
 
    const labels = this.serializer.toListenRequestLabels(queryData);
    if (labels) {
      request.labels = labels;
    }
 
    this.sendRequest(request);
  }
 
  /**
   * Unregisters interest in the results of the query associated with the
   * given targetId.
   */
  unwatch(targetId: TargetId): void {
    const request: ListenRequest = {};
    request.database = this.serializer.encodedDatabaseId;
    request.removeTarget = targetId;
    this.sendRequest(request);
  }
}
 
/** Listener for the PersistentWriteStream */
export interface WriteStreamListener extends PersistentStreamListener {
  /**
   * Called by the PersistentWriteStream upon a successful handshake response
   * from the server, which is the receiver's cue to send any pending writes.
   */
  onHandshakeComplete: () => Promise<void>;
 
  /**
   * Called by the PersistentWriteStream upon receiving a StreamingWriteResponse
   * from the server that contains a mutation result.
   */
  onMutationResult: (
    commitVersion: SnapshotVersion,
    results: MutationResult[]
  ) => Promise<void>;
}
 
/**
 * A Stream that implements the Write RPC.
 *
 * The Write RPC requires the caller to maintain special streamToken
 * state in between calls, to help the server understand which responses the
 * client has processed by the time the next request is made. Every response
 * will contain a streamToken; this value must be passed to the next
 * request.
 *
 * After calling start() on this stream, the next request must be a handshake,
 * containing whatever streamToken is on hand. Once a response to this
 * request is received, all pending mutations may be submitted. When
 * submitting multiple batches of mutations at the same time, it's
 * okay to use the same streamToken for the calls to writeMutations.
 *
 * TODO(b/33271235): Use proto types
 */
export class PersistentWriteStream extends PersistentStream<
  api.WriteRequest,
  api.WriteResponse,
  WriteStreamListener
> {
  private handshakeComplete_ = false;
 
  constructor(
    queue: AsyncQueue,
    connection: Connection,
    credentials: CredentialsProvider,
    private serializer: JsonProtoSerializer
  ) {
    super(
      queue,
      TimerId.WriteStreamConnection,
      TimerId.WriteStreamIdle,
      connection,
      credentials
    );
  }
 
  /**
   * The last received stream token from the server, used to acknowledge which
   * responses the client has processed. Stream tokens are opaque checkpoint
   * markers whose only real value is their inclusion in the next request.
   *
   * PersistentWriteStream manages propagating this value from responses to the
   * next request.
   */
  lastStreamToken: ProtoByteString;
 
  /**
   * Tracks whether or not a handshake has been successfully exchanged and
   * the stream is ready to accept mutations.
   */
  get handshakeComplete(): boolean {
    return this.handshakeComplete_;
  }
 
  // Override of PersistentStream.start
  start(listener: WriteStreamListener): void {
    this.handshakeComplete_ = false;
    super.start(listener);
  }
 
  protected tearDown() {
    if (this.handshakeComplete_) {
      this.writeMutations([]);
    }
  }
 
  protected startRpc(
    token: Token | null
  ): Stream<api.WriteRequest, api.WriteResponse> {
    return this.connection.openStream<api.WriteRequest, api.WriteResponse>(
      'Write',
      token
    );
  }
 
  protected onMessage(responseProto: api.WriteResponse): Promise<void> {
    // Always capture the last stream token.
    assert(
      !!responseProto.streamToken,
      'Got a write response without a stream token'
    );
    this.lastStreamToken = responseProto.streamToken!;
 
    if (!this.handshakeComplete_) {
      // The first response is always the handshake response
      assert(
        !responseProto.writeResults || responseProto.writeResults.length === 0,
        'Got mutation results for handshake'
      );
      this.handshakeComplete_ = true;
      return this.listener!.onHandshakeComplete();
    } else {
      // A successful first write response means the stream is healthy,
      // Note, that we could consider a successful handshake healthy, however,
      // the write itself might be causing an error we want to back off from.
      this.backoff.reset();
 
      const results = this.serializer.fromWriteResults(
        responseProto.writeResults
      );
      const commitVersion = this.serializer.fromVersion(
        responseProto.commitTime!
      );
      return this.listener!.onMutationResult(commitVersion, results);
    }
  }
 
  /**
   * Sends an initial streamToken to the server, performing the handshake
   * required to make the StreamingWrite RPC work. Subsequent
   * calls should wait until onHandshakeComplete was called.
   */
  writeHandshake(): void {
    assert(this.isOpen(), 'Writing handshake requires an opened stream');
    assert(!this.handshakeComplete_, 'Handshake already completed');
    // TODO(dimond): Support stream resumption. We intentionally do not set the
    // stream token on the handshake, ignoring any stream token we might have.
    const request: WriteRequest = {};
    request.database = this.serializer.encodedDatabaseId;
    this.sendRequest(request);
  }
 
  /** Sends a group of mutations to the Firestore backend to apply. */
  writeMutations(mutations: Mutation[]): void {
    assert(this.isOpen(), 'Writing mutations requires an opened stream');
    assert(
      this.handshakeComplete_,
      'Handshake must be complete before writing mutations'
    );
    assert(
      this.lastStreamToken.length > 0,
      'Trying to write mutation without a token'
    );
 
    const request: WriteRequest = {
      // Protos are typed with string, but we support UInt8Array on Node
      // tslint:disable-next-line:no-any
      streamToken: this.lastStreamToken as any,
      writes: mutations.map(mutation => this.serializer.toMutation(mutation))
    };
 
    this.sendRequest(request);
  }
}